package com.tianji.promotion.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.domain.po.UserCoupon;
import com.tianji.promotion.enums.CouponStatus;
import com.tianji.promotion.mapper.CouponMapper;
import com.tianji.promotion.service.ICouponService;
import com.tianji.promotion.service.IUserCouponService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
@RequiredArgsConstructor
public class IssueCouponTask {


    @Autowired
    private ICouponService couponService;

    @Autowired
    private IUserCouponService userCouponService;


    @XxlJob(value = "beginIssueCouponJobHandler")
    public ReturnT<String> beginIssueCouponJobHandler(){
        log.info("XxlJob, beginIssueCouponJobHandler, 执行成功");
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        ArrayList<Long> ids = new ArrayList<>();
        Integer index=0;
        while (true){
            Page<Coupon> page = new Page<>(index*shardTotal+shardIndex+1,2);
            log.info("XxlJob, beginIssueCouponJobHandler, 执行成功, index*shardTotal+shardIndex+1:{}",index*shardTotal+shardIndex+1);
            Page<Coupon> couponPage = couponService.page(page);
            List<Coupon> coupons = couponPage.getRecords();
            if(CollUtil.isEmpty(coupons)){
                break;
            }
            for (Coupon coupon : coupons) {
                if(coupon.getIssueBeginTime()==null){
                    continue;
                }
                if(Objects.equals(coupon.getStatus(), CouponStatus.UN_ISSUE) && coupon.getIssueBeginTime().isBefore(LocalDateTime.now())){
                    ids.add(coupon.getId());
                    log.info("XxlJob, beginIssueCouponJobHandler, 执行成功, id:{}",coupon.getId());
                    log.info("XxlJob, beginIssueCouponJobHandler, 执行成功, name:{}",coupon.getName());
                }
            }
            index++;
        }
        couponService.update(new Coupon().setStatus(CouponStatus.ISSUING)
                        .setUpdateTime(LocalDateTime.now()),
                new LambdaQueryWrapper<Coupon>().in(Coupon::getId, ids));
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "EndIssueCouponJobHandler")
    public ReturnT<String> EndIssueCouponJobHandler(){
        log.info("XxlJob, EndIssueCouponJobHandler, 执行成功");
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        ArrayList<Long> ids = new ArrayList<>();
        Integer index=0;
        while (true){
            Page<Coupon> page = new Page<>(index*shardTotal+shardIndex+1,2);
            Page<Coupon> couponPage = couponService.page(page);
            List<Coupon> coupons = couponPage.getRecords();
            if(CollUtil.isEmpty(coupons)){
                break;
            }
            for (Coupon coupon : coupons) {
                if(coupon.getIssueEndTime()==null){
                    continue;
                }
                if(Objects.equals(coupon.getStatus(), CouponStatus.ISSUING) && coupon.getIssueEndTime().isBefore(LocalDateTime.now())){
                    ids.add(coupon.getId());
                }
            }
            index++;
        }
        couponService.update(new Coupon().setStatus(CouponStatus.FINISHED).setUpdateTime(LocalDateTime.now()),
                new LambdaQueryWrapper<Coupon>().in(Coupon::getId,ids));
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "CouponOverdueRemindJobHandler")
    public ReturnT<String> CouponOverdueRemindJobHandler(){
        List<UserCoupon> list = userCouponService.list();
        for (UserCoupon userCoupon : list) {
            LocalDateTime termEndTime = userCoupon.getTermEndTime();
            if( termEndTime!=null && termEndTime.isBefore(LocalDateTime.now().plus(1, ChronoUnit.DAYS))){
                log.info("id为{}的用户的优惠券即将过期",userCoupon.getUserId());
            }
        }
        return ReturnT.SUCCESS;
    }
}
